package Service;

import QQcommon.Massage_2;
import QQcommon.Message;



import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class Service_Thread extends Thread{
    Socket socket;
    private String userId;
    private static ArrayList<Message> arrayList=new ArrayList<>();
    private static ArrayList<Message> arrayList1=new ArrayList<>();
    public Service_Thread(Socket socket,String userId){
        this.socket=socket;
        this.userId=userId;
    }

    public Socket getSocket() {
        return socket;
    }

    @Override
    public void run() {
        while (true){
            System.out.println("服务端和客户端通信"+userId+"读取数据.....");
            try {
                ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
                Message message=(Message) ois.readObject();
                if(message.getMassageType().equals(Massage_2.MESSAGE_GET_ONLINE_FRIEND)){
                    System.out.println(message.getSender()+"要在线用户列表");
                    String olineUser=Massage_Thread.getOlineUser();
                    Message message1=new Message();
                    message1.setMassageType(Massage_2.MEAASGE_RET_ONLINE_FRIEND);
                    message1.setContent(olineUser);
                    message1.setSender(userId);
                    message1.setGetter(message.getSender());
                    //返回给客户端
                    ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
                    oos.writeObject(message1);
                }
                else if (message.getMassageType().equals(Massage_2.MESSAGE_CLIENT_EXIT)){
                    System.out.println(message.getSender()+"退出程序");
                    Massage_Thread.remove_Thread(userId);
                    socket.close();
                    break;

                } else if (message.getMassageType().equals(Massage_2.MESSAGE_COMM_MES)) {//私聊
                        if (Massage_Thread.service_thread(message.getGetter())==null){//对方离线
                            arrayList.add(message);
                            Service.setConcurrentHashMap1(message.getGetter(),arrayList);
                        }else {
                            ObjectOutputStream oos = new ObjectOutputStream(Massage_Thread.service_thread(message.getGetter()).socket.getOutputStream());
                            oos.writeObject(message.getContent());
                        }

                }else if (message.getMassageType().equals(Massage_2.MESSAGE_TO_ALL_MES)){
                    HashMap<String,Service_Thread> hm=Massage_Thread.getHm();
                    Iterator<String> iterator=hm.keySet().iterator();
                    while (iterator.hasNext()) {
                        String oline=iterator.next();
                        if (!oline.equals(userId)){
                            ObjectOutputStream oos = new ObjectOutputStream(Massage_Thread.service_thread(oline).socket.getOutputStream());
                            oos.writeObject(message.getContent());
                        }

                    }

                } else if (message.getMassageType().equals(Massage_2.MESSAGE_SEND_FILE)) {
                    if (Massage_Thread.service_thread(message.getGetter())==null){//对方离线
                        arrayList1.add(message);
                        Service.setConcurrentHashMap1(message.getGetter(),arrayList1);
                    }else {
                    ObjectOutputStream oos = new ObjectOutputStream(Massage_Thread.service_thread(message.getGetter()).socket.getOutputStream());
                    oos.writeObject(message.getContent());
                }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
